﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cow.Module;
using Cow.Handler;
using System.IO;

namespace Cow
{
    public class CodeFactory
    {
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="nameSpace"></param>
        /// <param name="outPutPath"></param>
        /// <param name="target"></param>
        public String CreateCode(String inputFile, String nameSpace, String outPutPath, CodeTarget target)
        {
            var message = String.Empty;
            try
            {
                //导入Excel
                IImportHandler importHandler = new ImportExcelHandler();
                Design design = importHandler.Import(inputFile);
                IExportHandler iExportHandler = null;
                String defaultNameSpace = String.Empty;
                switch (target)
                {
                    case CodeTarget.Entity:
                        defaultNameSpace = "Forest.Cat.Domain.Entities";
                        iExportHandler = new ExportEntityHandler();
                        break;
                    case CodeTarget.Map:
                        defaultNameSpace = "Forest.Cat.Domain.Repository.Mappings";
                        iExportHandler = new ExportMapHandler();
                        break;
                    case CodeTarget.Repository:
                        iExportHandler = new ExportRepositoryHandler();
                        break;
                    case CodeTarget.Schema:
                        iExportHandler = new ExportSchemaHandler();
                        break;
                    case CodeTarget.Service:
                        CreateCode(inputFile, nameSpace, outPutPath, CodeTarget.ServiceInterface);
                        defaultNameSpace = "Forest.Cat.Domain.Services.{0}s";
                        iExportHandler = new ExportServiceHandler();
                        break;
                    case CodeTarget.ServiceInterface:
                        defaultNameSpace = "Forest.Cat.Domain.Services.{0}s";
                        iExportHandler = new ExportServiceInterfaceHandler();
                        break;
                    case CodeTarget.ViewModel:
                        CreateCode(inputFile, nameSpace, outPutPath, CodeTarget.MappingExtensions);
                        defaultNameSpace = "Forest.Cat.Web.Models";
                        iExportHandler = new ExportViewModelHandler();
                        break;
                    case CodeTarget.MappingExtensions:
                        defaultNameSpace = "Forest.Cat.Web.Extensions";
                        iExportHandler = new ExportMappingExtensionsHandler();
                        break;
                }
                design.NameSpace = defaultNameSpace;
                if (!String.IsNullOrWhiteSpace(nameSpace))
                {
                    design.NameSpace = nameSpace;
                }
                //如果输出路径不存在
                if (!Directory.Exists(outPutPath))
                {
                    Directory.CreateDirectory(outPutPath);
                }
                design.OutputPath = outPutPath + "\\" + iExportHandler.PrefixName + design.ModuleName + iExportHandler.SuffixName;
                iExportHandler.Export(design);
            }
            catch
            {
                message = String.Format("FileName = {0}; Target = {1}", inputFile, target.ToString());
            }
            return message;
        }
    }
}
